<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//站点相关信息
class Special extends MY_Controller {
    public function __construct(){
        parent::__construct();
        //权限处理
        $this->sess_checked();
        //加载专题模型类
        $this->load->model('special_m');
        $this->load->model('specialContentType_m');
        $this->load->model('specialR_m');
        $this->load->model('column_m');
        $this->load->model('info_m');
    }
    //专题列表
    public function lists(){
        $where=array();
        if(isset($_POST['name']) && $_POST['name']!=''){
            $where['name']=array('like',$_POST['name']);
        }
        $arr=$this->special_m->ajaxall($where);
        $this->assign('list',$arr['list']);
        if(isset($_POST['ajaxs'])){
            $htmlcontent=$this->fetch('special/speciallists.html');
            $arr['list']=$htmlcontent;
             ajax_return(1,'',$arr);
        }
        $this->assign('pageshow',$arr['pageshow']);
        $this->display('special/index.html');
    }

    //专题数据验证
    private function special_data($data){
        if(!isset($data['name']) || $data['name']==''){ajax_return(0,'名称不能为空');}
        if(!isset($data['bgimgurl']) ||  trim($data['bgimgurl'])==''){ajax_return(0,'背景图片不能为空');}
    }
    //添加专题
    public function add(){
        if(isset($_POST['ajaxs'])) {
            $data = form_ajaxs($_POST['formjquery']);
            $this->special_data($data);
            $data['status'] = intval($data['status']);
            $eduadmininfo=$_SESSION['eduadmininfo'];
            $eduadmininfo=unserialize($eduadmininfo);
            $data['username'] = $eduadmininfo['displayname'];
            if ($addid = $this->special_m->add($data)) {
                $this->adminuserRecord_m->add(1, 1, '专题id为' . $addid);
                ajax_return(1, '添加专题成功');
            } else {
                $this->adminuserRecord_m->add(1, 2, '专题');
                ajax_return(0, '添加专题失败');
            }
        }else{
            $this->display('Special/add.html');
        }
    }
    //编辑专题
    public function edit(){
        if(isset($_POST['recommendflag_ajaxs'])){//推荐修改
            $where['id']=array('=',$_POST['id']);
            $data['recommend']=$_POST['recommend'];
            if($_POST['recommend']==1){$msg='是';}else{$msg='否';}
            if($this->special_m->edit($where,$data)!==false){
                $this->adminuserRecord_m->add(2,1,'专题id为'.$_POST['id']."推荐状态改为".$msg);
                ajax_return(1,"修改推荐成功");
            }else{
                $this->adminuserRecord_m->add(2,2,'专题id为'.$_POST['id']."推荐状态");
                ajax_return(0,'修改推荐失败');
            }
        }elseif(isset($_POST['status_ajaxs'])){//状态
            $where['id']=array('=',$_POST['id']);
            $data['status']=$_POST['status'];
            if($_POST['status']==1){$msg='开启';}else{$msg='关闭';}
            if($this->special_m->edit($where,$data)!==false){
                $this->adminuserRecord_m->add(2,1,'专题id为'.$_POST['id']."状态改为".$msg);
                ajax_return(1,"修改状态成功");
            }else{
                $this->adminuserRecord_m->add(2,2,'专题id为'.$_POST['id']."状态");
                ajax_return(0,'修改状态失败');
            }
        }elseif(isset($_POST['ajaxs'])){
            $data=form_ajaxs($_POST['formjquery']);
            $this->special_data($data);
            $where['id']=array('=', $data['id']);
            if($this->special_m->edit($where,$data)===false){
                $this->adminuserRecord_m->add(2,2,'专题id为'.$id);
                ajax_return(0,'编辑失败');
            }
            $this->adminuserRecord_m->add(2,1,'专题id为'.$data['id']);
            ajax_return(1,'编辑成功');
        }elseif(isset($_GET['id'])){
            $where['id']=array('=',$_GET['id']);
            $list = $this->special_m->sel($where);
            $this->assign('list', $list);
            $this->display('Special/edit.html');
        }else{
            $this->error_msg('参数错误');
        }
    }
    //删除专题
    public function del(){
        if(isset($_POST['ajaxs']) && isset($_POST['id'])){
            $id=$_POST['id'];
            $where['specialid'] = array('=', $id);
            if($this->specialContentType_m->sel($where)){
                ajax_return(0,'该专题下有内容分类，不能删除');
            }
            if($this->specialR_m->sel($where)){
                 ajax_return(0,'该专题有关联文章，不能删除');
            }
            $where_d['id']=array('=',$id);
            if($this->special_m->del($where_d)===false){
                 $this->adminuserRecord_m->add(3,2,'专题id为'.$id);
                ajax_return(0,'删除失败');
            }else{
                 $this->adminuserRecord_m->add(3,1,'专题id为'.$id);
                ajax_return(1,'删除成功');
            }
        }else{
            ajax_return(0,'参数错误');
        }
    }
    //专题分类列表
    public function contentTypeList()
    {
        $this->load->model('specialContentType_m');
        if(isset($_GET['id'])){
            $id=$_GET['id'];
            $where['A.specialid']=array('=',$id);
            $list=$this->specialContentType_m->selall($where);
            $this->assign('id',$id);
            $this->assign('list', $list);
            $this->display('special/contents_list.html');

        }else{
            $this->error_msg('参数错误');
        }
    }
    //删除专题内容分类
    public function delContentType(){
        if(isset($_POST['ajaxs']) && isset($_POST['id'])){
            //判断专题内容分类下是否还要文章
              $where_r['ctypeid']=array('=',$_POST['id']);
            if($this->specialR_m->sel($where_r)){
                ajax_return(0,'当前专题分类下存在文章，不能删除');
            }
            $where['id']=array('=',$_POST['id']);
            if($this->specialContentType_m->del($where)===false){
                $this->adminuserRecord_m->add(3,2,'专题分类id为'.$_POST['id']);
                ajax_return(0,'删除失败');
             }
             $this->adminuserRecord_m->add(3,1,'专题分类id为'.$_POST['id']);
            ajax_return(1,'删除成功');
        }else{
            ajax_return(0,'参数错误');
        }
    }
    //添加专题
    public function addContentType(){
        if(isset($_POST['ajaxs'])) {
            $data = form_ajaxs($_POST['formjquery']);
            if(!isset($data['ctypename']) || $data['ctypename']==''){ajax_return(0,'参数错');}
            //判断专题名称是否已经存在
            $where['specialid']=array('=',$data['specialid']);
            $where['ctypename']=array('=',$data['ctypename']);
            if($this->specialContentType_m->sel($where)){
                ajax_return(0,'当前专题已经存在'.$data['ctypename']);
            }

            $eduadmininfo=$_SESSION['eduadmininfo'];
            $eduadmininfo=unserialize($eduadmininfo);
            $data['userid'] = $eduadmininfo['id'];
            $data['rnum'] = 0;
            if ($addid = $this->specialContentType_m->add($data)) {
                $this->adminuserRecord_m->add(1, 1, '专题内容分类id为' . $addid);
                ajax_return(1, '添加专题内容分类成功', array('sid'=>$data['specialid']));
            } else {
                $this->adminuserRecord_m->add(1, 2, '专题内容分类');
                ajax_return(0, '添加专题内容分类失败');
            }
        }elseif(isset($_GET['id'])){
            $this->assign('id',$_GET['id']);
            $this->display('Special/contentType_add.html');
        }
    }
    //编辑专题内容分类
    public function editContentType()
    {
        if(isset($_POST['ajaxs'])) {
            $data=form_ajaxs($_POST['formjquery']);
            //判断专题名称是否已经存在
            $where['id']=array('!=', $data['id']);
            $where['specialid']=array('=',$data['specialid']);
            $where['ctypename']=array('=',$data['ctypename']);
            if($this->specialContentType_m->sel($where)){
                ajax_return(0,'当前专题已经存在'.$data['ctypename']);
            }
            $where_edit['id']=array('=',$data['id']);
            if($this->specialContentType_m->edit($where_edit,$data)===false){
                $this->adminuserRecord_m->add(2,2,'专题内容分类id为'.$data['id']);
                ajax_return(0,'编辑失败');
            }
            $this->adminuserRecord_m->add(2,1,'专题内容分类id为'.$data['id']);
            ajax_return(1,'编辑成功');
        }elseif(isset($_GET['id'])){
            $where=array();
            $where['id']=array('=',$_GET['id']);
            $list = $this->specialContentType_m->sel($where);
            $this->assign('list', $list);
            $this->display('Special/contentType_edit.html');
        }
    }
    private function contents_data($data)
    {
        if(!isset($data['ctypename'])){ajax_return(0,'数据错误');}
        if(trim($data['ctypename'])==''){ajax_return(0,'专题内容分类名称不能为空');}
    }

    //专题文章管理
    public function addArticles(){
        if(isset($_POST['infoarr'])){//给文章写入专题文章关联表
            $data=$_POST;
            $infoarr=explode(',',$data['infoarr']);
            //根据专题id 和 分类id 获取已经存在的文章
            $where_s_r['specialid']=array('=',$data['specialid']);
            $where_s_r['ctypeid']=array('=',$data['ctypeid']);
            $where_s_r['infoid']=array('in',$infoarr);
            $specialRlist=array();
            $specialRlist=$this->specialR_m->selall($where_s_r);
            if(!empty($specialRlist)){//去掉已经存在的专题id
                foreach($specialRlist as $k=>$v){
                    foreach($infoarr as $x=>$y){
                        if($v['infoid']==$y){
                            unset($infoarr[$k]);
                        }
                    }
                }
            }
            $infoarr=array_values($infoarr);
            //写入数据库
            $data_w=array();
            if(empty($infoarr)){ajax_return(1,'ok');}
            foreach($infoarr as $k=>$v){
                $data_w[$k]['specialid']=$data['specialid'];
                $data_w[$k]['ctypeid']=$data['ctypeid'];
                $data_w[$k]['infoid']=$v;
            }
            $infoidstr=implode(',',$infoarr);

            if($this->specialR_m->addmore($data_w)===false){
                  $this->adminuserRecord_m->add(1,2,'专题文章插入id为'.$infoidstr);
                  ajax_return(0,'添加失败');
            }else{
                 $this->adminuserRecord_m->add(1,1,'专题文章插入id为'.$infoidstr);
                  ajax_return(1,'添加成功');
            }

        }elseif(isset($_GET['id']) || isset($_POST['id'])){
            $specialid=isset($_GET['id'])?$_GET['id']:$_POST['id'];
            $this->assign('specialid',$specialid);
            //获取专题内容分类
            $where_contenttype['A.specialid']=array('=',$specialid);
            $contenttypelist=$this->specialContentType_m->selall($where_contenttype);
            if(empty($contenttypelist)){
                echo Js::alert('请添加专题分类');exit;
            }
            $this->assign('contenttypelist',$contenttypelist);
            //获取权限栏目
            $columnselected=$this->column_m->getColumnselected();
            $this->assign('columnselected',$columnselected);
            $where=array();
            if(isset($_POST['title']) && $_POST['title']!=''){
                $where['A.title']=array('like',$_POST['title']);
            }
            if(isset($_POST['colid'])){
                $where['A.colid']=array('=',$_POST['colid']);
            }
            $where['A.delflag']=array('=',0);
            $field='A.*,B.name as sitesname,C.name as columnname,D.displayname';
            if(isset($_POST['sortvalue'])){//排序
                $sortvalue=explode('_',$_POST['sortvalue']);
                if($sortvalue[1]==1){$desc=" desc";}else{$desc=" asc";}
                $order=$sortvalue[0].$desc;
                $order_default=$_POST['sortvalue'];
            }else{
                $order='addtime desc';
                $order_default='addtime_1';
            }
            $this->assign('sortvalue',$order_default);
            $arr=$this->info_m->ajaxall($where,$field,$order);
            //对文章判断是否已经添加进入专题中
                //1.获取当前页面文章id 集合
            $articleid=array();
            foreach ($arr['list'] as $key => $value) {
               $articleid[]=$value['infoid'];
            }
            //获取当前页已经存在的专题文章
            $where_s_r['specialid']=array('=',$specialid);
            $where_s_r['infoid']=array('in',$articleid);
            if(isset($_POST['ctypeid'])){
                $ctypeid=$_POST['ctypeid'];
                $where_s_r['ctypeid']=array('=',$_POST['ctypeid']);
            }else{
                 $ctypeid=$contenttypelist[0]['id'];
                 $where_s_r['ctypeid']=array('=',$ctypeid);
            }
            $this->assign('ctypeid',$ctypeid);
            $specialRlist=array();
            $specialRlist=$this->specialR_m->selall($where_s_r);
            $specialRlistarr=array();
            foreach ($specialRlist as $key => $value) {
                $specialRlistarr[]=$value['infoid'];
            }
           
            foreach ($arr['list'] as $k => $v) {
                if(in_array($v['infoid'],$specialRlistarr)){
                    $v['ischeckobx']=true;
                }else{
                    $v['ischeckobx']=false;
                }
                $arr['list'][$k]=$v;
                
            }
            //查询当前页面infoid 在专题文章关联表中的数据;
         
            $this->assign('list',$arr['list']);
            if(isset($_POST['ajaxs'])){
                $htmlcontent=$this->fetch('special/specialarticlelist.html');
                $arr['list']=$htmlcontent;
                 ajax_return(1,'',$arr);
            }
            $this->assign('pageshow',$arr['pageshow']);
            $this->display('special/articles.html');exit;
        }else{
            $this->error_msg('参数错误');
        }
    }
    //移除专题关联文章
    public function removeArticles(){
        $data=$_POST;
        $infoarr=explode(',',$data['infoarr']);
        //根据专题id 和 分类id 获取已经存在的文章
        $where_s_r['specialid']=array('=',$data['specialid']);
        $where_s_r['ctypeid']=array('=',$data['ctypeid']);
        $where_s_r['infoid']=array('in',$infoarr);
        $specialRlist=array();
        //获取选择已存在的文件数
        $specialRlist=$this->specialR_m->selall($where_s_r);
        if(empty($specialRlist)){ajax_return(1,'提交成功');}else{
            $data['num']=count($specialRlist);
        }
        if($this->specialR_m->removemore($where_s_r,$data)===false){
              $this->adminuserRecord_m->add(3,2,'专题文章id为'.$data['infoarr']);
              ajax_return(0,'移除失败');
        }else{
             $this->adminuserRecord_m->add(3,1,'专题文章id为'.$data['infoarr']);
              ajax_return(1,'移除成功');
        }
    }
    //专题数据验证
    private function article_data($data){
        if(!isset($data['ctypeid']) || !isset($data['specialid']) || !isset($data['articles'])){ajax_return(0,'数据错误');}
        if(intval($data['specialid']) <= 0){ajax_return(0,'添加文章失败');}
        if($data['articles']==''){ajax_return(0,'请先选择文章');}
    }
}

